import static org.junit.Assert.*;
import junit.framework.Assert;

import org.junit.Test;

public class CartTest {

	private Product addProductToCart(Cart cart, String name, double price)
	{
		Product product = new Product(name, price);
		cart.addProduct(product);
		return product;
	}

	@Test
	public void createEmptyCartAndCheckTotal()
	{
		Cart cart = new Cart();
		double total = cart.getTotal();
		assertEquals(0, total, 0);
	}
	
	@Test
	public void addTwoProductsToCartAndCheckTotal()
	{
		Cart cart = new Cart();
		addProductToCart(cart, "Eclipse", 5);
		addProductToCart(cart, "NetBeans", 5);
		double total = cart.getTotal();
		assertEquals(10, total, 0);
	}
	
	@Test
	public void addProductRemoveByNameAndCheckTotalZero()
	{
		Cart cart = new Cart();
		addProductToCart(cart, "Eclipse", 5);
		cart.removeProduct("Eclipse");
		double total = cart.getTotal();
		assertEquals(0, total, 0);
	}
	
	@Test
	public void addProductRemoveByObjectAndCheckTotalZero()
	{
		Cart cart = new Cart();
		Product p = addProductToCart(cart, "Eclipse", 5);
		cart.removeProduct(p);
		double total = cart.getTotal();
		assertEquals(0, total, 0);
	}
	
	@Test
	public void addUbuntuAndCheck15PercentDiscount()
	{
		Cart cart = new Cart();
		addProductToCart(cart, "Ubuntu", 10);
		addProductToCart(cart, "Ubuntu", 10);
		
		addProductToCart(cart, "Adobe Premiere", 80);
		double total = cart.getTotal();
		assertEquals(85, total, 0);
	}

	@Test
	public void addAndRemoveUbuntuAndCheckNoDiscount()
	{
		Cart cart = new Cart();
		addProductToCart(cart, "Ubuntu", 10);
		addProductToCart(cart, "Adobe Premiere", 100);
		cart.removeProduct("Ubuntu");
		double total = cart.getTotal();
		assertEquals(100, total, 0);
	}
	
	@Test
	public void addFedoraAndCheck12PercentDiscount()
	{
		Cart cart = new Cart();
		addProductToCart(cart, "Fedora", 10);
		
		addProductToCart(cart, "Adobe Premiere", 90);
		double total = cart.getTotal();
		assertEquals(88, total, 0);
	}

	@Test
	public void addFedora_addUbuntu_removeUbuntu_AndCheck12PercentDiscount()
	{
		Cart cart = new Cart();
		Product ubu = addProductToCart(cart, "Ubuntu", 5);
		addProductToCart(cart, "Fedora", 10);
		cart.removeProduct(ubu);
		addProductToCart(cart, "Adobe Premiere", 90);
		double total = cart.getTotal();
		assertEquals(88, total, 0);
	}

	@Test
	public void addWin7AndCheck10PercentDiscount()
	{
		Cart cart = new Cart();
		addProductToCart(cart, "Win7", 1000);
		addProductToCart(cart, "Adobe Premiere", 100);
		double total = cart.getTotal();
		assertEquals((1000+100)*0.9, total, 0);
	}

	@Test
	public void addAndRemoveWin7AndCheckNoDiscount()
	{
		Cart cart = new Cart();
		addProductToCart(cart, "Win7", 10);
		addProductToCart(cart, "Adobe Premiere", 100);
		cart.removeProduct("Win7");
		double total = cart.getTotal();
		assertEquals(100, total, 0);
	}

	@Test
	public void addFedora_addWin7_removeWin7_AndCheck12PercentDiscount()
	{
		Cart cart = new Cart();
		Product w7 = addProductToCart(cart, "Win7", 5);
		addProductToCart(cart, "Fedora", 10);
		cart.removeProduct(w7);
		addProductToCart(cart, "Adobe Premiere", 90);
		double total = cart.getTotal();
		assertEquals((10+90)*(0.88), total, 0);
	}

}
